﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Data;

namespace TestMiniAccess
{
    using Ruandao.MiniAccess;
    using Ruandao.MiniAccess.OrMap;
    using Ruandao.MiniAccess.SqlServer;
    using System.Configuration;

    [TestClass()]
    public class ProcedureBuilderTest
    {

        string tableName;
        TableMap tableMap;

        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试属性
        [TestInitialize()]
        public void MyTestInitialize()
        {
            string connString = ConfigurationManager.ConnectionStrings["TestMiniAccessConnection"].ConnectionString;
            Database database = new SqlDatabase(connString);
            tableName = "ForeignKeyTable";
            tableMap = new TableMap( "ForeignKeyTable", database );
            tableMap.SetDefaultTableMap( database, CommandType.StoredProcedure,true);
       }
        
        #endregion

        [TestMethod()]
        public void GetDeleteProcedureNameTest()
        {
            string tableName = "table";

            string expected = "Delete_table";
            string actual;
            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetDeleteProcedureName(tableName);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetDeleteProcedureName 未返回所需的值。");
        }

        [TestMethod()]
        public void GetFetchProcedureNameTest()
        {
            string tableName = "table";

            string expected = "Select_table_By_PrimaryKey";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetFetchProcedureName(tableName);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetFetchProcedureName 未返回所需的值。");
        }

        [TestMethod()]
        public void GetInsertProcedureNameTest()
        {
            string tableName = "table name";

            string expected = "Insert_table_name";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetInsertProcedureName(tableName);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetInsertProcedureName 未返回所需的值。");
        }

        [TestMethod()]
        public void GetSearchProcedureNameTest()
        {
            string tableName = "Table Name";

            string expected = "Select_Table_Name_By_Conditions";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetSearchProcedureName(tableName);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetSearchProcedureName 未返回所需的值。");
        }


        [TestMethod()]
        public void GetUpdateProcedureNameTest()
        {
            string tableName = "Table Name";

            string expected = "Update_Table_Name";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetUpdateProcedureName(tableName);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.GetUpdateProcedureName 未返回所需的值。");
        }

        [TestMethod()]
        public void TableNameForProcedureTest()
        {
            string s = "Word Word Word";

            string expected = "Word_Word_Word";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.TableNameForProcedure(s);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.OrMapHelper.TableNameForProcedure 未返回所需的值。");
        }

        [TestMethod()]
        public void GetDeleteProcedureTextTest()
        {
            string expected =
@"CREATE PROCEDURE DeleteProc
	@CompositeKey1 nvarchar(100),
	@CompositeKey2 nvarchar(100),
	@CompositeKey3 nvarchar(100)
AS
DELETE FROM [ForeignKeyTable]
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
            string actual;
            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetDeleteProcedureText(tableName, tableMap.ColumnMaps, "DeleteProc");

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.ProcedureBuilder.GetDeleteProcedure 未返回所需的值。");
        }

        [TestMethod()]
        public void GetFetchProcedureTextTest()
        {
            string expected =
@"CREATE PROCEDURE FetchProc
	@CompositeKey1 nvarchar(100),
	@CompositeKey2 nvarchar(100),
	@CompositeKey3 nvarchar(100)
AS
SELECT
	[ForeignKeyTable].[CompositeKey1],
	[ForeignKeyTable].[CompositeKey2],
	[ForeignKeyTable].[CompositeKey3],
	[ForeignKeyTable].[PrimaryKeyId],
	[ForeignKeyTable].[TableId],
	[ForeignKeyTable].[GuidField],
	[ForeignKeyTable].[Field1],
	[ForeignKeyTable].[Field2]
FROM [ForeignKeyTable]
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetFetchProcedureText(tableName, tableMap.ColumnMaps, "FetchProc");

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.ProcedureBuilder.GetFetchProcedure 未返回所需的值。");
        }

        [TestMethod()]
        public void GetInsertProcedureTextTest()
        {
            string expected =
@"CREATE PROCEDURE InsertProc
	@CompositeKey1 nvarchar(100),
	@CompositeKey2 nvarchar(100),
	@CompositeKey3 nvarchar(100),
	@PrimaryKeyId nvarchar(100),
	@TableId int,
	@GuidField uniqueidentifier,
	@Field1 int,
	@Field2 int
AS
INSERT INTO [ForeignKeyTable] (
	[CompositeKey1],
	[CompositeKey2],
	[CompositeKey3],
	[PrimaryKeyId],
	[TableId],
	[GuidField],
	[Field1],
	[Field2]
)
VALUES
(
	@CompositeKey1,
	@CompositeKey2,
	@CompositeKey3,
	@PrimaryKeyId,
	@TableId,
	@GuidField,
	@Field1,
	@Field2
)
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetInsertProcedureText(tableName, tableMap.ColumnMaps, "InsertProc", tableMap.IdentityColumn);

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.ProcedureBuilder.GetInsertProcedure 未返回所需的值。");
        }

        [TestMethod()]
        public void GetSearchProcedureTextTest()
        {
            string expected =
@"CREATE PROCEDURE SearchProc
	@Conditions NVarChar(800)=null
AS
DECLARE @N NVarChar(2000);
SET @N=N'SELECT
	[ForeignKeyTable].[CompositeKey1],
	[ForeignKeyTable].[CompositeKey2],
	[ForeignKeyTable].[CompositeKey3],
	[ForeignKeyTable].[PrimaryKeyId],
	[ForeignKeyTable].[TableId],
	[ForeignKeyTable].[GuidField],
	[ForeignKeyTable].[Field1],
	[ForeignKeyTable].[Field2]
	FROM [ForeignKeyTable] '+ @Conditions;
EXECUTE sp_executesql @N
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetSearchProcedureText(tableName, tableMap.ColumnMaps, "SearchProc");

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.ProcedureBuilder.GetSearchProcedure 未返回所需的值。");
        }

        [TestMethod()]
        public void GetUpdateProcedureTextTest()
        {
            string expected =
@"CREATE PROCEDURE UpdateProc
	@New_CompositeKey1 nvarchar(100),
	@New_CompositeKey2 nvarchar(100),
	@New_CompositeKey3 nvarchar(100),
	@PrimaryKeyId nvarchar(100),
	@TableId int,
	@GuidField uniqueidentifier,
	@Field1 int,
	@Field2 int,
	@CompositeKey1 nvarchar(100),
	@CompositeKey2 nvarchar(100),
	@CompositeKey3 nvarchar(100)
AS
UPDATE [ForeignKeyTable]
SET
	[CompositeKey1] = @New_CompositeKey1,
	[CompositeKey2] = @New_CompositeKey2,
	[CompositeKey3] = @New_CompositeKey3,
	[PrimaryKeyId] = @PrimaryKeyId,
	[TableId] = @TableId,
	[GuidField] = @GuidField,
	[Field1] = @Field1,
	[Field2] = @Field2
WHERE
	[CompositeKey1] = @CompositeKey1 AND
	[CompositeKey2] = @CompositeKey2 AND
	[CompositeKey3] = @CompositeKey3
";
            string actual;

            actual = Ruandao.MiniAccess.SqlServer.ProcedureBuilder_Accessor.GetUpdateProcedureText(tableName, tableMap.ColumnMaps, "UpdateProc");

            Assert.AreEqual(expected, actual, "Ruandao.MiniDac.SqlServer.ProcedureBuilder.GetUpdateProcedure 未返回所需的值。");
        }
    }
}
